package com.service;

import static com.def.GenericDef.E_BIS_OPE;
import static com.def.GenericDef.E_READ_NOT_FOUND;
import static com.def.GenericDef.E_XPLSTEQP_INVALID_INPUT;
import static com.def.GenericDef.E_XPLSTEQP_READ_BIS_OPE_ERROR;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;
import static com.def.GenericDef.T_XPLSTEQP;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dao.IBisDataDao;
import com.dao.IBisOpeDao;
import com.dao.IBisToolDao;
import com.dao.IBisToolGrDao;
import com.dao.IXpinqcodDao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_data;
import com.model.Bis_ope;
import com.model.Bis_opeId;
import com.model.Bis_tool;
import com.model.Bis_tool_gr;
import com.rtnBean.NotFoundRtnBeen;
import com.tx.Xplsteqp.XplsteqpI;
import com.tx.Xplsteqp.XplsteqpO;
import com.tx.Xplsteqp.XplsteqpOA;
import com.tx.Xplsteqp.XplsteqpOA2;
import com.tx.Xplsteqp.XplsteqpOA3;
import com.tx.Xprma.XprmaI;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XplsteqpService")
public class XplsteqpService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XplsteqpService.class);
	private static String TX_NAME = T_XPLSTEQP;

	@Autowired
	private IXpinqcodDao queryDao;
	@Autowired
	private IBisToolGrDao bisToolGrDao;
	@Autowired
	private IBisToolDao bisToolDao;
	@Autowired
	private IBisOpeDao bisOpeDao;
	@Autowired
	private XplsteqpI xplsteqpI;
	@Autowired
	private XplsteqpO xplsteqpO;
	@Autowired
	private IBisDataDao bisDataDao;
	
	public String subMainProc(String strInMsg) {
		MqOpe mqOpe1 = new MqOpe();
		xplsteqpI = new XplsteqpI();
		xplsteqpO = new XplsteqpO();
		String reply = "";
		try {
			xplsteqpI = (XplsteqpI) mqOpe1.inStr2Obj(strInMsg, xplsteqpI);
			logger.info("InTrx is " + strInMsg);
			xplsteqpO.setTrx_id(TX_NAME);
			xplsteqpO.setTrx_id("O");
			String actionFlg = xplsteqpI.getAction_flg();
			if (actionFlg.equals("I")) {
				xplsteqpO = getToolList(xplsteqpI.getBay_id());
			} else if (actionFlg.equals("O")) { // By ope
				xplsteqpO = getToolListByOpe(xplsteqpI.getOpe_id(),
						xplsteqpI.getOpe_ver());
			} else if (actionFlg.equals("G")) { // Buff tool group
				xplsteqpO = getBuffToolGroupInfo();
			} else if (actionFlg.equals("B")) { // Buff tool
				xplsteqpO = getBuffToolInfo(xplsteqpI.getRoot_tool_id());
			} else if(actionFlg.equals("L")){// By ope && user
				xplsteqpO = getToolListByOpeUser();
			} else if(actionFlg.equals("S")){//By user && bay
				xplsteqpO = getToolListByBayUser();
			}else if(actionFlg.equals("F")){//By user && bay
				xplsteqpO = getToolListByOpeUserFab();
			}else {
				xplsteqpO.setRtn_mesg("Invalide type input: [" + actionFlg
						+ "]");
				xplsteqpO.setRtn_code(String.valueOf(E_XPLSTEQP_INVALID_INPUT));
			}
		} catch (Exception ex) {
			xplsteqpO.setTrx_id(TX_NAME);
			xplsteqpO.setTrx_id("O");
			xplsteqpO.setRtn_code(RETURN_CODE_UNKNOWN);
			xplsteqpO.setRtn_mesg(ex.getMessage());
			logger.error(Util.stackTraceToString(ex));
			return reply;
		}

		xplsteqpO.setTrx_id(TX_NAME);
		xplsteqpO.setTrx_id("O");
		reply = mqOpe1.obj2Str(xplsteqpO);
		return reply;
	}

	private XplsteqpO getBuffToolGroupInfo() {
		XplsteqpO xplsteqpO = new XplsteqpO();
		List<String> rootToolList = new ArrayList<String>();
		List<XplsteqpOA2> oaryBuffG = new ArrayList<XplsteqpOA2>();

		Bis_ope ope = new Bis_ope();

		String sqlOpe = "from Bis_ope where proc_id='PGGY' ";
		ope = (Bis_ope) bisOpeDao.comTblRead(sqlOpe);
		if (ope == null) {
			xplsteqpO.setRtn_mesg("Read bis_ope [proc_id='PGGY'] error!");
			xplsteqpO.setRtn_code(String.valueOf(E_XPLSTEQP_READ_BIS_OPE_ERROR));
			return xplsteqpO;
		}

		String sqlToolgD = "select  distinct id.tool_id_fk from Bis_tool_gr  where  id.toolg_id in (     "
				+ "select  toolg_id from Bis_ope   where  proc_id='PGGY'     "
				+ ") ";
		rootToolList = bisToolDao.findDistStringList(sqlToolgD);

		if (!rootToolList.isEmpty()) {
			for (String root_tool_id : rootToolList) {
				if (!Util.isSpaceCheck(root_tool_id)) {
					XplsteqpOA2 xplsteqpOA2 = new XplsteqpOA2();
					xplsteqpOA2.setRoot_tool_id(root_tool_id);
					oaryBuffG.add(xplsteqpOA2);
				}
			}
			xplsteqpO.setOaryBuffG(oaryBuffG);
			xplsteqpO.setTbl_cnt(String.valueOf(oaryBuffG.size()));
		} else {
			xplsteqpO.setTbl_cnt("0");
		}

		xplsteqpO.setRtn_code(RETURN_CODE_OK);

		return xplsteqpO;
	}

	private XplsteqpO getBuffToolInfo(String root_tool_id) {
		XplsteqpO xplsteqpO = new XplsteqpO();
		List<Bis_tool> bis_ToolList = new ArrayList<Bis_tool>();
		List<XplsteqpOA3> oaryBuffT = new ArrayList<XplsteqpOA3>();

		if(Util.isSpaceCheck(root_tool_id)){
			xplsteqpO.setTbl_cnt("0");
			xplsteqpO.setRtn_code(RETURN_CODE_OK);
			return xplsteqpO;
		}
		
		String sqlTool = "from Bis_tool where root_tool_id='" + root_tool_id + "' ";
		bis_ToolList = bisToolDao.find(sqlTool);
		if(bis_ToolList.isEmpty()){
			xplsteqpO.setTbl_cnt("0");
		}else{
			for(Bis_tool tool :bis_ToolList ){
				XplsteqpOA3 xplsteqpOA3 = new XplsteqpOA3();
				xplsteqpOA3.setTool_id(tool.getTool_id());
				
				oaryBuffT.add(xplsteqpOA3);
			}
			xplsteqpO.setOaryBuffT(oaryBuffT);
			xplsteqpO.setTbl_cnt(String.valueOf(oaryBuffT.size()));
		}

		xplsteqpO.setRtn_code(RETURN_CODE_OK);
		return xplsteqpO;
	}

	/**
	 * Query tool by ope id & ver
	 * 
	 * @param ope_id
	 * @param ope_ver
	 * @return
	 */
	private XplsteqpO getToolListByOpe(String ope_id, String ope_ver) {
		XplsteqpO xplsteqpO = new XplsteqpO();
		Bis_ope bis_ope = new Bis_ope();
		Bis_opeId bis_opeId = new Bis_opeId();
		bis_opeId.setOpe_id(ope_id);
		bis_opeId.setOpe_ver(ope_ver);
		bis_ope = bisOpeDao.get(bis_opeId);
		if (null == bis_ope) {
			xplsteqpO.setTrx_id("XPLSTEQP");
			xplsteqpO.setTrx_id("O");
			xplsteqpO.setRtn_mesg(new NotFoundRtnBeen(bis_opeId.toString(),
					Bis_ope.class).toString());
			xplsteqpO.setRtn_code(String.valueOf(E_BIS_OPE + E_READ_NOT_FOUND));
			return xplsteqpO;
		}

		List<Bis_tool_gr> toolGList = new ArrayList<Bis_tool_gr>();
		String sql = "FROM Bis_tool_gr where id.toolg_id='"
				+ bis_ope.getToolg_id() + "'";
		toolGList = bisToolGrDao.find(sql);
		List<XplsteqpOA> oaryList = new ArrayList<XplsteqpOA>();
		for (Bis_tool_gr toolG : toolGList) {
			XplsteqpOA oary = new XplsteqpOA();
			oary.setTool_id(toolG.getId().getTool_id_fk());
			oaryList.add(oary);
		}
		xplsteqpO.setOary(oaryList);
		xplsteqpO.setTbl_cnt(Integer.toString(oaryList.size()));
		xplsteqpO.setTrx_id("XPLSTEQP");
		xplsteqpO.setType_id("O");
		xplsteqpO.setRtn_code("0000000");
		xplsteqpO.setRtn_mesg("success");
		return xplsteqpO;
	}

	public XplsteqpO getToolList(String bay_id) {
		XplsteqpO xplsteqpO = new XplsteqpO();
		List<Bis_tool> toolList = new ArrayList<Bis_tool>();
		String sql = "from Bis_tool where bay_id='" + bay_id + "'";
		toolList = queryDao.find(sql);
		List<XplsteqpOA> oaryList = new ArrayList<XplsteqpOA>();
		for (Bis_tool tool : toolList) {
			XplsteqpOA oary = new XplsteqpOA();
			oary.setTool_id(tool.getTool_id());
			oary.setTool_cate(tool.getTool_cate());
			oary.setTool_dsc(tool.getTool_dsc());
			oary.setRoot_tool_id(tool.getRoot_tool_id());
			oary.setUnit_typ(tool.getUnit_typ());
			oary.setBay_id(tool.getBay_id());
			oary.setFloor_code(tool.getFloor_code());
			oaryList.add(oary);
		}
		xplsteqpO.setOary(oaryList);
		xplsteqpO.setTbl_cnt(Integer.toString(oaryList.size()));
		xplsteqpO.setTrx_id("XPLSTEQP");
		xplsteqpO.setType_id("O");
		xplsteqpO.setRtn_code("0000000");
		xplsteqpO.setRtn_mesg("success");
		return xplsteqpO;
	}
	private XplsteqpO getToolListByOpeUser() {
		int i =0;
		String ope_id="",ope_ver="",user_id="",toog_id="";
		List<XplsteqpOA>oaryList= new ArrayList<XplsteqpOA>();
		List<Bis_tool_gr> toolGList = new ArrayList<Bis_tool_gr>();
		ope_id = xplsteqpI.getOpe_id();
		ope_ver = xplsteqpI.getOpe_ver();
		user_id = xplsteqpI.getUser_id();
		Bis_ope bis_ope = new Bis_ope();
		Bis_opeId bis_opeId = new Bis_opeId();
		bis_opeId.setOpe_id(ope_id);
		bis_opeId.setOpe_ver(ope_ver);
		bis_ope = bisOpeDao.get(bis_opeId);
		if (null == bis_ope) {
			xplsteqpO.setRtn_mesg("这个站点["+ope_id+"]不存在，请确认！");
			xplsteqpO.setRtn_code(String.valueOf(E_BIS_OPE + E_READ_NOT_FOUND));
			return xplsteqpO;
		}
		toog_id = bis_ope.getToolg_id();
		String admin_sql = "FROM Bis_data where data_cate='UFUG'AND DATA_EXT='"+user_id+
				"' AND EXT_1='ADMIN'AND EXT_3='Y'";
		List<Bis_data> UFUG_list = bisDataDao.find(admin_sql);
		if(UFUG_list.size()==1){
			String sql = "FROM Bis_tool_gr where id.toolg_id='"
					+ toog_id + "'";
			toolGList = bisToolGrDao.find(sql);
		}else{
//			String sql = "SELECT * FROM BIS_TOOL_GR A JOIN BIS_DATA B ON A.TOOL_ID_FK = B.EXT_1 " +
//					"WHERE A.TOOLG_ID ='"+toog_id+"'AND B.DATA_CATE IN('UREQ')AND B.DATA_EXT='"+user_id+
//					"'ORDER BY CONVERT(EXT_2,UNSIGNED)";
//			toolGList = bisToolGrDao.findBySQL(sql, Bis_tool_gr.class);
			String sql = "FROM Bis_tool_gr where id.toolg_id='"  //Mosie temp all 2014/10/12
					+ toog_id + "'";                             //Mosie temp all 2014/10/12
			toolGList = bisToolGrDao.find(sql);                  //Mosie temp all 2014/10/12
		}
		for (Bis_tool_gr toolG : toolGList) {
			XplsteqpOA oary = new XplsteqpOA();
			oary.setTool_id(toolG.getId().getTool_id_fk());
			oaryList.add(oary);
		}
		xplsteqpO.setOary(oaryList);
		xplsteqpO.setTbl_cnt(Integer.toString(oaryList.size()));
		xplsteqpO.setRtn_code(RETURN_CODE_OK);
		xplsteqpO.setRtn_mesg(RETURN_MESG_OK);
		return xplsteqpO;
	}
	
	private XplsteqpO getToolListByOpeUserFab() {
		String ope_id="",ope_ver="",user_id="",toog_id="";
		String fl_flg="N";
		List<XplsteqpOA>oaryList= new ArrayList<XplsteqpOA>();
		List<Bis_tool_gr> toolGList = new ArrayList<Bis_tool_gr>();
		ope_id = xplsteqpI.getOpe_id();
		ope_ver = xplsteqpI.getOpe_ver();
		user_id = xplsteqpI.getUser_id();
		Bis_ope bis_ope = new Bis_ope();
		Bis_opeId bis_opeId = new Bis_opeId();
		bis_opeId.setOpe_id(ope_id);
		bis_opeId.setOpe_ver(ope_ver);
		bis_ope = bisOpeDao.get(bis_opeId);
		if (null == bis_ope) {
			xplsteqpO.setRtn_mesg("这个站点["+ope_id+"]不存在，请确认！");
			xplsteqpO.setRtn_code(String.valueOf(E_BIS_OPE + E_READ_NOT_FOUND));
			return xplsteqpO;
		}
		if("JBPG".equals(bis_ope.getProc_id())){
			fl_flg = "Y";
		}
		toog_id = bis_ope.getToolg_id();
		String admin_sql = "FROM Bis_data where data_cate='UFUG' AND DATA_EXT = '" + user_id + "' AND EXT_1 = 'ADMIN' AND EXT_3 = 'Y' ";
		List<Bis_data> UFUG_list = bisDataDao.find(admin_sql);
		if(UFUG_list.size()==1&&fl_flg.endsWith("N")){
			String sql = "FROM Bis_tool_gr where id.toolg_id = '"	+ toog_id + "'";
			toolGList = bisToolGrDao.find(sql);
		}else{

			String sql = "SELECT * FROM BIS_TOOL_GR WHERE TOOLG_ID='"+toog_id+"'";
						  
			toolGList = bisToolGrDao.findBySQL(sql, Bis_tool_gr.class);                
		}
		for (Bis_tool_gr toolG : toolGList) {
			XplsteqpOA oary = new XplsteqpOA();
			oary.setTool_id(toolG.getId().getTool_id_fk());
			oaryList.add(oary);
		}
		xplsteqpO.setOary(oaryList);
		xplsteqpO.setTbl_cnt(Integer.toString(oaryList.size()));
		xplsteqpO.setRtn_code(RETURN_CODE_OK);
		xplsteqpO.setRtn_mesg(RETURN_MESG_OK);
		return xplsteqpO;
	}
	private XplsteqpO getToolListByBayUser() {
		int i =0;
		String user_id="",bay_id="";
		List<XplsteqpOA>oaryList= new ArrayList<XplsteqpOA>();
		user_id = xplsteqpI.getUser_id();
		bay_id = xplsteqpI.getBay_id();
		List<Bis_tool> bisToolList = new ArrayList<Bis_tool>();
		String admin_sql = "FROM Bis_data where data_cate='UFUG'AND DATA_EXT='"+user_id+
				"' AND EXT_1='ADMIN'AND EXT_3='Y'";
		List<Bis_data> UFUG_list = bisDataDao.find(admin_sql);
		if(UFUG_list.size()==1){
			String sql = "from Bis_tool where bay_id='" + bay_id + "'";
			bisToolList = bisToolDao.find(sql);
		}else{
//			String sql = "SELECT * FROM BIS_TOOL A JOIN BIS_DATA B ON A.TOOL_ID = B.EXT_1 WHERE " +
//					 "A.BAY_ID ='"+bay_id+"'AND B.DATA_CATE IN('UREQ')AND B.DATA_EXT='"+user_id+"'";
//			bisToolList = bisToolDao.findBySQL(sql, Bis_tool.class);
			String sql = "from Bis_tool where bay_id='" + bay_id + "'";  //Mosie temp add 2014/10/12
			bisToolList = bisToolDao.find(sql);  //Mosie temp add 2014/10/12
		}
		for(i=0;i<bisToolList.size();i++){
			XplsteqpOA oary = new XplsteqpOA();
			Bis_tool bis_tool = bisToolList.get(i);
			oary.setTool_id(bis_tool.getTool_id());
			oaryList.add(oary);
		}
		xplsteqpO.setOary(oaryList);
		xplsteqpO.setTbl_cnt(Integer.toString(oaryList.size()));
		xplsteqpO.setRtn_code(RETURN_CODE_OK);
		xplsteqpO.setRtn_mesg(RETURN_MESG_OK);
		return xplsteqpO;
	}

}
